package com.frxuexi.park2301.user

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.park2301.R
import com.frxuexi.park2301.databinding.ActivityRegisterBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlin.random.Random

@Route(path = "/app/RegisterActivity")
@AndroidEntryPoint
class RegisterActivity : BaseActivity<ActivityRegisterBinding,UserViewModel>() {
    override fun initData() {
        vm.registerData.observe(this){
            Log.i("===",it.toString())
            if(it>0){
                ToastUtils.showLong("注册成功")
                finish()
            }
        }
    }

    override fun initView() {
        binding.registerSendcode.setOnClickListener {
            val num=Math.round(Math.random()*1000000)
            Log.i("===",num.toString())
            binding.registerCode.setText(num.toString())
//            ToastUtils.showLong("验证码：")
        }

        binding.registerCode.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if(s?.length!=0){
                    binding.btnRegister.isEnabled=true
                }else{
                    binding.btnRegister.isEnabled=false
                }
            }

            override fun afterTextChanged(s: Editable?) {
            }

        })

        binding.btnRegister.setOnClickListener {
            val uname = binding.registerUname.text.toString()
            val pass = binding.registerPass.text.toString()
            val pass2 = binding.registerPass2.text.toString()

            if(uname.isNullOrEmpty() || pass.isNullOrEmpty()||pass2.isNullOrEmpty()){
                ToastUtils.showLong("输入内容不能为空")
                return@setOnClickListener
            }
            if(pass!=pass2){
                ToastUtils.showLong("两次密码不一致")
                return@setOnClickListener
            }
//            1[0-9]{10}
            if (!uname.matches(Regex("[A-z0-9_]{2,8}"))){
                ToastUtils.showLong("不是合法用户名")
                return@setOnClickListener
            }

            vm.register(uname,pass)
        }
    }



}